home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 May: Tool Chest / Developer CD Series May 1996 (Tool Chest) (Apple Computer) (1996).iso / Sample Code / Snippets / Testing & Debugging / Audit / Src / AuditDocument.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-21  |  9.7 KB  |  430 lines  |  [TEXT/KAHL]

  1. /*                                    AuditDocument.c                                */
  2. /*
  3.  * DisplayAudit.c
  4.  * Copyright © 1992-93, Apple Computer Inc. All Rights Reserved.
  5.  * Programmed by Martin Minow,
  6.  *    Internet:    minow@apple.com
  7.  *    AppleLink:    MINOW
  8.  */
  9. #include "DisplayAudit.h"
  10.  
  11. #ifndef TESTING
  12. #define TESTING                    0
  13. #endif
  14. #if TESTING
  15. void            TestAuditStatusLocationUsingAnExtremelyLongFunctionName(void);
  16. pascal void        TestPascalVoidFunction(void);
  17. pascal short    TestPascalShortFunction(void);
  18. pascal OSErr    TestPascalFunctionWithParameters(Ptr, Ptr);
  19. #endif
  20.  
  21. /*
  22.  * MakeDocumentWindow creates a window that will hold the log information.
  23.  * It then creates/initializes the Audit Record for that window. The strings
  24.  * should be fetched from the resource fork.
  25.  */
  26. void
  27. MakeDocumentWindow(void)
  28. {
  29.         OSErr                            status;
  30.         register DocumentPtr            documentPtr;
  31.         WindowPtr                        theWindow;
  32.         short                            windowWidth;
  33.         short                            windowHeight;
  34.         Point                            windowTopLeft;
  35.         Rect                            windowShape;
  36.         SFReply                            sfReply;
  37.         Str255                            windowTitle;
  38.         short                            listFontNumber;
  39.         StringPtr                        message;
  40.         Boolean                            saveAuditEnable;
  41.         
  42.         documentPtr = (DocumentPtr) NewPtrClear(sizeof (DocumentRecord));
  43.         FailNIL(documentPtr, kErrNoMemory);
  44.         TRY {
  45.             AuditFileDialog(
  46.                 DLOG_SFPutFile,
  47.                 "\pAudit Log File",
  48.                 "\pAudit Log",
  49.                 &sfReply        
  50.             );
  51.             gParameterUpdateNeeded = TRUE;
  52.             DOC.auditIdent = PARM.auditIdent;
  53.             DOC.enableAudit = PARM.enableAudit;
  54.             DOC.scanCount = PARM.auditRecords;
  55.             if (gHasColorQuickDraw) {
  56.                 theWindow = GetNewCWindow(
  57.                                 WIND_Document,
  58.                                 &DOC.windowRecord,
  59.                                 (WindowPtr) -1
  60.                             );
  61.             }
  62.             else {
  63.                 theWindow = GetNewWindow(
  64.                                 WIND_Document,
  65.                                 &DOC.windowRecord,
  66.                                 (WindowPtr) -1
  67.                             );
  68.             }
  69.             FailNIL(theWindow, kErrCreateTextWindow);
  70.             if ((long) theWindow != (long) documentPtr)        /* Can't happen        */
  71.                 FailNIL(NULL, kErrCreateTextWindow);        /* It did!            */
  72.             GetWTitle(WINDOW_PTR, windowTitle);
  73.             BlockMove(
  74.                 &DOC.auditIdent,
  75.                 &windowTitle[windowTitle[0] + 1 - sizeof (OSType)],
  76.                 sizeof (OSType)
  77.             );
  78.             SetWTitle(WINDOW_PTR, windowTitle);
  79.             windowWidth = width(qd.screenBits.bounds) / 2;
  80.             if (windowWidth < kMinWindowWidth)
  81.                 windowWidth = kMinWindowWidth;
  82.             windowHeight = height(qd.screenBits.bounds) - (GetMBarHeight() * 2) - 4;
  83.             if (windowHeight < kMinWindowHeight)
  84.                 windowHeight = kMinWindowHeight;
  85.             windowTopLeft.h = qd.screenBits.bounds.right - windowWidth;
  86.             windowTopLeft.v = qd.screenBits.bounds.top + GetMBarHeight() * 2 + 2;
  87.             MoveWindow(WINDOW_PTR, windowTopLeft.h, windowTopLeft.v, FALSE);
  88.             SizeWindow(WINDOW_PTR, windowWidth, windowHeight, FALSE);
  89.             BringToFront(WINDOW_PTR);
  90.             ShowWindow(WINDOW_PTR);
  91.             SelectWindow(WINDOW_PTR);
  92.             SetPort(WINDOW_PTR);
  93.             ValidRect(&WINDOW.portRect);                    /* Eliminate update    */
  94.             /*
  95.              * Build a display log in the window.
  96.              * To do: let the user choose the font and size.
  97.              */
  98.             GetFNum(PARM.fontName, &listFontNumber);
  99.             windowShape = WINDOW.portRect;
  100.             windowShape.right -= kScrollBarOffset;
  101.             windowShape.bottom -= kScrollBarOffset;
  102.             DOC.logListHandle = CreateLog(
  103.                     &windowShape,
  104.                     TRUE,                                    /* Has grow box        */
  105.                     listFontNumber,
  106.                     PARM.fontSize,
  107.                     PARM.logDisplayLines
  108.                 );
  109.             FailNIL(DOC.logListHandle, kErrCreateTextWindow);
  110.             DrawGrowIcon(WINDOW_PTR);
  111.             /*
  112.              * Create a log file if requested. (Error is non-fatal).
  113.              */            
  114.             if (sfReply.good == FALSE)
  115.                 DOC.logFileRefNum = 0;
  116.             else {
  117.                 status = CreateOutputFile(
  118.                             'ttxt',
  119.                             sfReply.fName,
  120.                             sfReply.vRefNum,
  121.                             &DOC.logFileRefNum
  122.                         );
  123.                 if (status != noErr) {
  124.                     DOC.logFileRefNum = 0;
  125.                     ErrorAlert(status, kErrCreateOutputFile, FALSE);
  126.                 }
  127.                 else {
  128.                     DOC.logFileVRefNum = sfReply.vRefNum;
  129.                     pstrcpy(DOC.logFileName, sfReply.fName);
  130.                 }
  131.             }
  132.             DOC.auditPtr = GetAuditPtr(DOC.auditIdent);
  133.             if (DOC.auditPtr != NULL)
  134.                 message = "\pAudit Record was previously created";
  135.             else {
  136.                 DOC.auditPtr = InitAudit(
  137.                             DOC.auditIdent,
  138.                             PARM.auditRecords,
  139.                             DOC.enableAudit,
  140.                             FALSE
  141.                         );
  142.                 message = "\pAudit Record created";
  143.             }
  144.             GetCurrentProcess(&DOC.oldPSN);
  145.             WakeUpAudit(DOC.auditPtr, &DOC.oldPSN);
  146.             saveAuditEnable = EnableAudit(DOC.auditPtr, TRUE);
  147.             Audit(
  148.                 DOC.auditPtr,
  149.                 'APPL',
  150.                 AuditFormat3(
  151.                     kAuditFormatHex,
  152.                     kAuditFormatAddress,
  153.                     kAuditFormatString
  154.                 ),
  155.                 DOC.auditIdent,
  156.                 DOC.auditPtr,
  157.                 message
  158.             );
  159.             EnableAudit(DOC.auditPtr, saveAuditEnable);
  160. #if TESTING
  161.             AuditString(
  162.                 DOC.auditPtr,
  163.                 'Moof',
  164.                 "\p1234567890123456789012345678901234567890"
  165.             );
  166.             AuditStatusString(DOC.auditPtr, 'Moof', 1, "\pHello world");
  167.             AuditStatusLocation(DOC.auditPtr, 'Moof', 2);
  168.             TestAuditStatusLocationUsingAnExtremelyLongFunctionName();
  169.             TestPascalVoidFunction();
  170.             TestPascalShortFunction();
  171.             TestPascalFunctionWithParameters(NULL, NULL);
  172. #endif
  173.         }
  174.         CATCH {
  175.             if (DOC.logListHandle != NULL)
  176.                 DisposeLog(DOC.logListHandle);
  177.             if (DOC.logFileRefNum != 0) {
  178.                 (void) FSClose(DOC.logFileRefNum);
  179.                 (void) FSDelete(DOC.logFileName, DOC.logFileVRefNum);
  180.             }
  181.             DisposePtr((Ptr) documentPtr);
  182.         }
  183.         ENDTRY;
  184. }
  185.  
  186. void
  187. ProcessAuditDocument(
  188.         register DocumentPtr            documentPtr
  189.     )
  190. {
  191.         register short            i;
  192.         
  193.         for (i = 0; i < DOC.scanCount && ReadAudit(DOC.auditPtr, &ENTRY); i++)
  194.             ProcessAuditEntry(documentPtr);
  195. }
  196.  
  197. void
  198. ProcessAuditEntry(
  199.         register DocumentPtr            documentPtr
  200.     )
  201. {
  202.         Str255                            message;
  203.         Str255                            work;
  204.         short                            i;
  205.         
  206.         if (ENTRY.lostData > 0) {
  207.             DOC.totalMissedCount += ENTRY.lostData;
  208.             NumToString(ENTRY.lostData, message);
  209.             pstrcat(message, "\p log entries(");
  210.             NumToString(DOC.totalMissedCount, work);
  211.             pstrcat(message, work);
  212.             pstrcat(message, "\p total) missed...");
  213.             DisplayLogString(DOC.logListHandle, message);
  214.             DOC.logIndex += ENTRY.lostData;
  215.         }
  216.         DOC.logIndex += 1;
  217.         message[0] = 0;
  218.         NumToString(DOC.logIndex, work);
  219.         for (i = work[0]; i < 3; i++)
  220.             pstrcat(message, "\p ");
  221.         pstrcat(message, work);
  222.         pstrcat(message, "\p ");
  223.         FormatAuditEntryTimestamp(DOC.auditPtr, &ENTRY, work);
  224.         /*
  225.          * Since we know that the timestamp has a fixed-length format, we can chop
  226.          * out the date to gain some space in the text window. Note the format:
  227.          *        0        1         2
  228.          *        12345678901234567890123
  229.          *        1993.01.14 22:36:57.123
  230.          */
  231.         work[11] = work[0] - 11;
  232.         pstrcat(message, &work[11]);
  233.         pstrcat(message, "\p ");
  234.         FormatAuditEntryData(&ENTRY, work);
  235.         pstrcat(message, work);
  236.         DisplayLogString(DOC.logListHandle, message);
  237.         if (DOC.logFileRefNum != 0) {
  238.             TRY {
  239.                 WriteAuditOutputLine(message, DOC.logFileRefNum);
  240.             }
  241.             CATCH {
  242.                 ErrorAlert(STATUS, MESSAGE, FALSE);
  243.                 GetIndString(work, STRN_Messages, kErrWriteOutputFile);
  244.                 AuditStatusString(DOC.auditPtr, 'APPL', STATUS, work);
  245.                 CloseAuditOutputFile(
  246.                     STATUS,
  247.                     DOC.logFileRefNum,
  248.                     DOC.logFileVRefNum,
  249.                     DOC.logFileName
  250.                 );
  251.                 DOC.logFileRefNum = 0;
  252.                 NO_PROPAGATE;
  253.                 gUpdateMenusNeeded = TRUE;
  254.             }
  255.             ENDTRY;
  256.         }
  257. }
  258.  
  259. /*
  260.  * These are passed more-or-less intact to the log manager.
  261.  */
  262. void
  263. DoWindowKeyDown(
  264.         register DocumentPtr            documentPtr
  265.     )
  266. {
  267. #pragma unused (documentPtr)
  268.         /* Nothing happens here */
  269. }
  270.  
  271. void
  272. DoContentClick(
  273.         register DocumentPtr            documentPtr
  274.     )
  275. {
  276.         DoClickInLog(DOC.logListHandle, &EVENT);
  277. }
  278.  
  279. void
  280. UpdateDocumentWindow(
  281.         register DocumentPtr            documentPtr
  282.     )
  283. {
  284.         DrawGrowIcon(WINDOW_PTR);
  285.         UpdateLog(DOC.logListHandle);
  286. }
  287.  
  288. void
  289. ActivateDocumentWindow(
  290.         register DocumentPtr            documentPtr,
  291.         Boolean                            isActivating
  292.     )
  293. {
  294.         ActivateLog(DOC.logListHandle, isActivating);
  295. }
  296.  
  297. void
  298. DisposeDocumentWindow(
  299.         register DocumentPtr            documentPtr
  300.     )
  301. {
  302.         OSErr                        status;
  303.         short                        message;
  304.         
  305.         status = noErr;
  306.         if (DOC.auditPtr != NULL)
  307.             WakeUpAudit(DOC.auditPtr, &DOC.oldPSN);
  308.         if (DOC.logFileRefNum != 0) {
  309.             TRY {
  310.                 CloseAuditOutputFile(
  311.                     noErr,
  312.                     DOC.logFileRefNum,
  313.                     DOC.logFileVRefNum,
  314.                     DOC.logFileName
  315.                 );
  316.             }
  317.             CATCH {
  318.                 status = STATUS;
  319.                 message = MESSAGE;
  320.                 NO_PROPAGATE;
  321.             }
  322.             ENDTRY;
  323.         }
  324.         CloseWindow(WINDOW_PTR);
  325.         DisposePtr((Ptr) documentPtr);
  326.         if (--gOpenWindowCount <= 0)
  327.             gQuitNow = TRUE;
  328.         FailOSErr(status, message);
  329. }
  330.  
  331. void
  332. DecorateWindow(
  333.         register DocumentPtr            documentPtr
  334.     )
  335. {
  336.         SizeLog(
  337.             DOC.logListHandle,
  338.             width(WINDOW.portRect) - kScrollBarOffset,
  339.             height(WINDOW.portRect) - kScrollBarOffset
  340.         );
  341. }
  342.  
  343. void
  344. DoDocumentSaveAs(
  345.         register DocumentPtr            documentPtr
  346.     )
  347. {
  348.         if (DOC.logFileRefNum != 0)
  349.             DoDocumentCloseFile(documentPtr);
  350.         TRY {
  351.             PromptAndCreateAuditOutputFile(
  352.                 "\pAudit Output File",
  353.                 "\pAudit Log",
  354.                 'ttxt',
  355.                 &DOC.logFileRefNum,
  356.                 &DOC.logFileVRefNum,
  357.                 DOC.logFileName
  358.             );
  359.         }
  360.         CATCH {
  361.             ErrorAlert(STATUS, MESSAGE, FALSE);
  362.             NO_PROPAGATE;
  363.         }
  364.         ENDTRY;
  365. }
  366.  
  367. void
  368. DoDocumentCloseFile(
  369.         register DocumentPtr            documentPtr
  370.     )
  371. {
  372.         TRY {
  373.             CloseAuditOutputFile(
  374.                 noErr,
  375.                 DOC.logFileRefNum,
  376.                 DOC.logFileVRefNum,
  377.                 DOC.logFileName
  378.             );
  379.         }
  380.         CATCH {
  381.             ErrorAlert(STATUS, MESSAGE, FALSE);
  382.             NO_PROPAGATE;
  383.         }
  384.         ENDTRY;
  385.         DOC.logFileRefNum = 0;
  386.         gUpdateMenusNeeded = TRUE;
  387. }
  388.  
  389. #if TESTING
  390. /*
  391.  * These were used to test the Audit library and are retained because I'm a kind
  392.  * of sentimental guy.
  393.  */
  394. #define AuditLocation(ident) (                            \
  395.         Audit(                                            \
  396.             ((DocumentPtr) FrontWindow())->auditPtr,    \
  397.             (ident),                                    \
  398.             AuditFormat1(kAuditFormatLocation)            \
  399.         )                                                \
  400.     )
  401.  
  402. void
  403. TestAuditStatusLocationUsingAnExtremelyLongFunctionName(void) {
  404.         AuditLocation('Moof');
  405. }
  406.  
  407. pascal void
  408. TestPascalVoidFunction(void)
  409. {
  410.         AuditLocation('Moof');
  411. }
  412.  
  413. pascal short
  414. TestPascalShortFunction(void)
  415. {
  416.         AuditLocation('Moof');
  417.         return (123);
  418. }
  419. pascal OSErr
  420. TestPascalFunctionWithParameters(
  421.         Ptr                        fooPtr,
  422.         Ptr                        barPtr
  423.     )
  424. {
  425.         AuditLocation('Moof');
  426.         return ((fooPtr == barPtr) ? noErr : paramErr);
  427. }
  428. #endif
  429.  
  430.